package com.zhangck.bos2_0.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhangck.bos2_0.Service.StandardService;

import cn.itcast.bos.domain.base.Standard;

/** 
* @author 上午茶
* @version 创建时间：2018年5月28日 下午3:41:47 
* 类说明 
*/
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {

	private Standard standatrd = new Standard();
	
	@Override
	public Standard getModel() {
		return standatrd;
	}
	@Autowired
	private StandardService StandardService;
	//保存方法
	@Action(value="StandardAction_save",results={@Result(name="success", type="redirect", location="/pages/base/standard.html")})
	public String save(){
		StandardService.save(standatrd);
		
		System.out.println("保存成功");
		return  "success";
	}
	
	private String row;
	
	public void setRow(String row) {
		this.row = row;
	}
	//删除方法
	@Action(value="StandardAction_delete",results={@Result(name="success", type="redirect", location="/pages/base/standard.html")})
	public String delete(){
		String row = ServletActionContext.getRequest().getParameter("row");
		System.out.println(row);
		String[] split = row.split("-");
		System.out.println(split.length);
		for (int i = 0; i < split.length; i++) {
			int parseInt = Integer.parseInt(split[i]);
			StandardService.delete(parseInt);
		}
		return  "success";
	}
	
	
	//
	@Action(value="StandardAction_find",results={@Result(name="success",type="json")})
	public String  StandardAction_find(){
	  List<Standard> list= StandardService.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return "success";
	}
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	//分页
	@Action(value="StandardAction_page",results={@Result(name="success",type="json")})
	public String  StandardAction_page(){
		Pageable request = new PageRequest(page-1, rows);
		Page<Standard> page1 = StandardService.finddatapage(request);

		Map<String, Object> map = new HashMap();
		map.put("total", page1.getTotalElements());//必须得用total接受
		map.put("rows", page1.getContent());
		
		
		
		ActionContext.getContext().getValueStack().push(map);
		
		return  "success";
	}
}
